package model

import (
	"errors"
	"gorm.io/gorm"
	"time"
)

type (
	ClientProjectModel interface {
		Insert(client Client) (err error)
		FindOneByClientId(client Client) (Client, error)
		FindOneByProjectId(client Client) (Client, error)
		Update(client Client) (err error)
		DeleteSoft(client Client) (err error)
		Delete(client Client) (err error)
	}

	DefaultClientProjectModel struct {
		conn  *gorm.DB
		table string
	}

	Client struct {
		Client_id         int       `db:"client_id"  json:"client_id"`
		Client_name       string    `db:"client_name"  json:"client_name"`
		Client_Telephone  string    `db:"client_telephone"  json:"client_telephone"`
		Client_Project_id int       `db:"client_project_id"  json:"client_project_id"`
		Create_time       time.Time `db:"create_time"  json:"create_time"`
		Delete_time       time.Time `db:"delete_time"  json:"delete_time"`
		DelState          int       `db:"del_state"  json:"del_state"`
	}
)

func NewDefaultClientProjectModel(conn *gorm.DB) *DefaultClientProjectModel {
	return &DefaultClientProjectModel{
		conn:  conn,
		table: "client",
	}
}

func (cli *DefaultClientProjectModel) Insert(client Client) (err error) {
	client.Create_time = time.Now()
	client.Delete_time = time.Unix(0, 0)
	cli.conn.Create(&client)
	return err
}

func (cli *DefaultClientProjectModel) FindOneByClientId(client Client) (Client, error) {
	var err error
	var count int64
	cli.conn.Model(&client).Where("client_id=?", client.Client_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return Client{}, err
	}
	cli.conn.Where("client_id=?", client.Client_id).First(&client)

	return client, nil
}

func (cli *DefaultClientProjectModel) FindOneByProjectId(client Client) (Client, error) {
	var err error
	var count int64
	cli.conn.Model(&client).Where("client_project_id=?", client.Client_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return Client{}, err
	}
	cli.conn.Where("client_project_id=?", client.Client_id).First(&client)

	return client, nil
}

func (cli *DefaultClientProjectModel) Update(client Client) (err error) {
	var count int64
	cli.conn.Model(&client).Where("client_id=?", client.Client_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return err
	}
	cli.conn.Model(&client).Where("client_id=?", client.Client_id).Updates(&client)
	return nil
}

func (cli *DefaultClientProjectModel) DeleteSoft(client Client) (err error) {
	client.Delete_time = time.Now()
	client.DelState = 1
	var count int64
	cli.conn.Model(&client).Where("client_id=?", client.Client_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return err
	}
	err = cli.Update(client)
	if err != nil {
		return err
	}
	return nil
}

func (cli *DefaultClientProjectModel) Delete(client Client) (err error) {
	var count int64
	cli.conn.Model(&client).Where("client_id=?", client.Client_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return err
	}
	cli.conn.Model(&client).Where("client_id=?", client.Client_id).Delete(&client)
	return nil
}
